﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Faster.Entity;
using System.Xml;
using System.IO;
using Faster.GameUtility;

namespace Faster.GameBLL
{
    public class UserInfoHelper
    {
        public UserInfo User { get; set; }

        public UserInfoHelper()
        {

        }

        public void GetUserInfo()
        {
            


            string url = "http://s1.kaixin001pvz.youkia.com/pvz/index.php/default/user/sig/e0b82de7701995d3112baf7b3f2f962b?1322729707326";

            string xml = RequestHelper.GetRequest(url, this.User.Cookies);

            //XmlDocument doc = new XmlDocument();
            //doc.LoadXml(xml);
            //XmlNode usernode = doc.SelectSingleNode("/root/user");
            LoadUserInfo(xml);
        }

        public void LoadUserInfo(string xml)
        {
            TextReader text = new StringReader(xml);
            XmlTextReader read = new XmlTextReader(text);
            read.WhitespaceHandling = WhitespaceHandling.All;
            while(read.Read())
            {
                if (read.Name == "user")
                {
                    for (int i = 0; i < read.AttributeCount; i++)
                    {
                        read.MoveToAttribute(i);
                        if (read.Name == "name")
                        {
                            this.User.UserName = read.Value;
                        }
                        else if (read.Name == "id")
                        {
                            this.User.UserID =  Convert.ToInt32(read.Value);
                        }
                        else if (read.Name == "face")
                        {
                            this.User.FaceUrl = read.ReadContentAsString();
                        }
                    }
                    read.MoveToElement();
                }
                else if (read.Name == "item")
                {
                    FriendInfo friend = new FriendInfo();
                    for (int i = 0; i < read.AttributeCount; i++)
                    {
                        read.MoveToAttribute(i);
                        if (read.Name == "name")
                        {
                            friend.FriendName= read.Value;
                        }
                        else if (read.Name == "id")
                        {
                            friend.FriendID = read.ReadContentAsInt();
                        }
                        else if (read.Name == "grade")
                        {
                            friend.FriendLevel = read.ReadContentAsInt();
                        }   
                        else if (read.Name == "face")
                        {
                            string face = read.ReadContentAsString();
                            if(face.StartsWith("/"))
                            {
                                face = "http://s1.kaixin001pvz.youkia.com"+face;
                            }
                            friend.FriendFace = face;
                        }
                    }
                    read.MoveToElement();
                    if (this.User.Friends == null)
                    {
                        this.User.Friends = new List<FriendInfo>();
                    }
                    this.User.Friends.Add(friend);
                }
            }
            read.Close();
            text.Close();
        }

        public void Watering(int friendid)
        {
            AMFPostData amfpostdata = new AMFPostData(AMFType.AMF3, 0, 1);
            amfpostdata.AddTargetAndResponse("api.garden.removeStateAll", "/1");

            AMFPostDataBody amfpostdatabody = new AMFPostDataBody(DataType.Array, 2);
            amfpostdatabody.AddData(DataType.Number, friendid.ToString());
            amfpostdatabody.AddData(DataType.Number, "1");

            amfpostdata.AddBody(amfpostdatabody);

            byte[] date = amfpostdata.Message.ToArray();

            AMFMessage message = RequestHelper.PostRequest("http://s1.kaixin001pvz.youkia.com/pvz/amf/", this.User.Cookies,date);

        }

        public void Fertilize(int friendid)
        {
            AMFPostData amfpostdata = new AMFPostData(AMFType.AMF3, 0, 1);
            amfpostdata.AddTargetAndResponse("api.garden.removeStateAll", "/1");

            AMFPostDataBody amfpostdatabody = new AMFPostDataBody(DataType.Array, 2);
            amfpostdatabody.AddData(DataType.Number, friendid.ToString());
            amfpostdatabody.AddData(DataType.Number, "2");

            amfpostdata.AddBody(amfpostdatabody);

            byte[] date = amfpostdata.Message.ToArray();

            AMFMessage message = RequestHelper.PostRequest("http://s1.kaixin001pvz.youkia.com/pvz/amf/", this.User.Cookies, date);

        }

        public void Gain(int friendid)
        {
            AMFPostData amfpostdata = new AMFPostData(AMFType.AMF3, 0, 1);
            amfpostdata.AddTargetAndResponse("api.garden.outAndStealAll", "/1");

            AMFPostDataBody amfpostdatabody = new AMFPostDataBody(DataType.Array, 1);
            amfpostdatabody.AddData(DataType.Number, friendid.ToString());

            amfpostdata.AddBody(amfpostdatabody);

            byte[] date = amfpostdata.Message.ToArray();

            AMFMessage message = RequestHelper.PostRequest("http://s1.kaixin001pvz.youkia.com/pvz/amf/", this.User.Cookies,date);

        }
    }
}
